package com.ruantu.clothing.controller.PCPandian;

import com.ruantu.clothing.controller.RukuController;
import com.ruantu.clothing.entity.JsonResponseMessage;
import com.ruantu.clothing.entity.PageBean;
import com.ruantu.clothing.entity.User;
import com.ruantu.clothing.entity.wxpandian.Pandian;
import com.ruantu.clothing.service.pcpandian.PCPandianService;
import com.ruantu.clothing.util.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/pcpandian")
public class PCPandianController {
    private static final Logger logger = LoggerFactory.getLogger(RukuController.class);
    @Autowired
    private PCPandianService pcPandianService;



    /**
     * 查询盘点记录
     * @param //jg_id 机构id
     * @return
     */
    @RequestMapping("selectPandianTable")
    @ResponseBody
    public JsonResponseMessage selectPandianTable(String pdid,HttpSession session){
        User user = (User) session.getAttribute("info");
        String jg_id =user.getJgid();
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(pcPandianService.selectPandianTable(jg_id,pdid));
        return jsonResponseMessage;
    }
//========================================优化===============================================================================


    /**
     * 当前盘点查看，包括已盘点和未盘点产品(盘点子表暂无记录)
     * @param selectText
     * @param //jg_id
     * @param //start
     * @param //rows
     * @return
     */
   /* @RequestMapping("selectNowPandianXQAndPdzbIsNull")
    @ResponseBody
    public JsonResponseMessage selectNowPandianXQAndPdzbIsNull(String selectText,PageBean pageBean,HttpSession session){
        String sText=selectText.replaceAll("[\\p{Punct}\\s]+", "");
        User user = (User) session.getAttribute("info");
        String jg_id =user.getJgid();
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(pcPandianService.selectNowPandianXQAndPdzbIsNull(sText,jg_id,pageBean));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        return jsonResponseMessage;
    }*/


    /**
     * 当前盘点查看，包括已盘点和未盘点产品(盘点子表已存在记录)
     * @param selectText
     * @param //jg_id
     * @param //pdid
     * @param //start
     * @param// rows
     * @return
     */
    @RequestMapping("selectNowPandianXQAndPdzbIsExist")
    @ResponseBody
    public JsonResponseMessage selectNowPandianXQAndPdzbIsExist(String selectText,PageBean pageBean,HttpSession session){
        String sText=selectText.replaceAll("[\\p{Punct}\\s]+", "");
        User user = (User) session.getAttribute("info");
        String jg_id =user.getJgid();
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(pcPandianService.selectNowPandianXQAndPdzbIsExist(sText,jg_id,pageBean));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        return jsonResponseMessage;
    }
    /**
     * 查询当前盘底是否已经存在盘点子表记录(0为没有)
     * @param //pdid
     * @return
     */
    @RequestMapping("selectPdjlByPdid")
    @ResponseBody
    public Integer selectPdjlByPdid(HttpSession session){
        User user = (User) session.getAttribute("info");
        String jg_id =user.getJgid();
        return pcPandianService.selectPdjlByPdid(jg_id);
    }
//===========================================================================================================================

    /**
     *根据盘点ID查询本机构已盘点详情(分页
     * @param pandian_id 盘点id
     * @param selectText 查询条件
     * @param pageBean
     * @return
     */
    @RequestMapping("selectAlreadyPDXQByPdid")
    @ResponseBody
    public JsonResponseMessage selectAlreadyPDXQByPdid(String pandian_id,String selectText,PageBean pageBean){
        String sText=selectText.replaceAll("[\\p{Punct}\\s]+", "");
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(pcPandianService.selectAlreadyPDXQByPdid(pandian_id,sText,pageBean));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        return jsonResponseMessage;
    }

    /**
     * 获取当前正在盘点id（用于判断当前盘点是否正在进行）
     * @param
     * @return
     */
    @RequestMapping("selectNowPDID")
    @ResponseBody
    public String selectNowPDID(HttpSession session){
        User user = (User) session.getAttribute("info");
        String jg_id =user.getJgid();
        return pcPandianService.selectNowPDID(jg_id);
    }

    /**
     * 开启新的盘点
     * @param //pdid  盘点ID
     * @param //pandianming 盘点名称
     * @param //start_time 开启时间
     * @param //jg_id 机构ID
     */
    @RequestMapping("startNewPandian")
    @ResponseBody
    public Integer startNewPandian(String pandianming,String start_time,HttpSession session){
        User user = (User) session.getAttribute("info");
        String jg_id =user.getJgid();
        String pdid= UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
        pcPandianService.startNewPandian(pdid,pandianming,start_time,jg_id);
        return 1;
    }

    /**
     * 结束盘点——修改盘点结束时间以及状态
     * 200----成功   400---失败
     * @param endime
     * @param pdid
     * @return
     */
    @RequestMapping("updatePandianTable")
    @ResponseBody
    public JsonResponseMessage updatePandianTable(String endime,String pdid){
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            pcPandianService.pandianEnd(endime,pdid);
            jsonResponseMessage.setCode(200);
        }catch (MyException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (NullPointerException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请重新登录");
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请刷新重新尝试");
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }

}


























